package com.xxh.auth.handler;

import com.xxh.common.utils.RedisUtil;
import com.xxh.dataaccess.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义登录成功处理器，主要是
 * 1 将用户权限信息存储到缓存，并且根据用户名生成token令牌
 * 2 用户登录状态用缓存统一管理。
 */
@Component
public class CustomAuthenticationSuccessHandler  implements AuthenticationSuccessHandler {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    UserService userService;



    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

        //取得用户信息
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        //生成令牌
//        String token = jwtTokenUtil.generateToken(userDetails);

        //获取权限，放入缓存。我不想做这一步

        //返回登录成功结果（携带token令牌）


    }
}
